package tjs.study.notes.eureka.clienta.user;

import com.codingapi.txlcn.tc.config.EnableDistributedTransaction;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.client.RestTemplate;
import tjs.study.notes.eureka.clienta.user.service.feign.log.FeignInterceptorConfig;

@EnableEurekaClient
@SpringBootApplication
@Configuration
@EnableFeignClients(defaultConfiguration = FeignInterceptorConfig.class) // 开启@FeignClient扫描
@EnableHystrix
// 开启分布式事务
@EnableDistributedTransaction(enableTxc = false)
@EnableTransactionManagement
public class UserApplication implements CommandLineRunner {
    public static String INSTANCE_ID = "";

    /**
     * Feign代理生成
     * {@link org.springframework.cloud.openfeign.FeignClientFactoryBean#getObject()}
     * <p>
     * Feign切面
     * {@link feign.ReflectiveFeign.FeignInvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])}
     *
     * @param args
     */
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(UserApplication.class, args);
        INSTANCE_ID = applicationContext.getEnvironment().getProperty("eureka.instance.instance-id");
    }

    @Bean
    @LoadBalanced//这个restRemplate开启负载均衡的功能
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();

        // 设置超时时间
        requestFactory.setConnectTimeout(2000);//连接建立时间，三次握手完成时间
        requestFactory.setReadTimeout(2000);//传递数据的超时时间

        return new RestTemplate(requestFactory);
    }

    @Bean("jdbcTemplate")
    public JdbcTemplate initJdbcTemplate() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(this.initBasicDataSource());
        return jdbcTemplate;
    }

    @Bean
    public DataSourceTransactionManager initTransactionManager() {
        org.springframework.jdbc.datasource.DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(this.initBasicDataSource());
        return transactionManager;
    }

    @Bean("dataSource")
    public BasicDataSource initBasicDataSource() {
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setUrl("jdbc:mysql://localhost:3306/tjs_mydemo");
        basicDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        basicDataSource.setUsername("root");
        basicDataSource.setPassword("qx123456");
        return basicDataSource;
    }

    @Autowired
    private Environment environment;

    @Override
    public void run(String... args) throws Exception {
        System.out.println("config.tag :" + environment.getProperty("config.tag"));
        System.out.println("local.base :" + environment.getProperty("local.base"));
        System.out.println("spring.application.name:" + environment.getProperty("spring.application.name"));
    }
}
